<template>
  <div>
    <!-- table -->
    <a-card style="margin-top: 24px">
      <div class="table-operator">
        <div>
          <a-range-picker @change="handleClickChangeDate" :style="{marginLeft:'10px',width: '256px'}" />
          <a-input-search style="margin-left:10px;width:280px;" :placeholder="this.$t('dataList.sszh')" @search="onSearch" enterButton />
        </div>
      </div>
      <a-table
        :columns="columns"
        :dataSource="data"
        :pagination="false"
        :loading="dataLoading"
        :rowSelection="{onChange: onSelectChange}"
      >
      </a-table>
      <div style="margin-top:15px;">
        <MyPage :curr="page.curr" :size="page.size" :all="page.total" @changePage="handleChangePage" @changePageSize="handleChangePageSize"/>
      </div>
    </a-card>
  </div>
</template>

<script>
import HeadInfo from '@/components/tools/HeadInfo'
import { mixin, mixinDevice } from '@/utils/mixin'
import { ACCESS_TOKEN } from '@/store/mutation-types'
import MyPage from './Page'

export default {
  name: 'AdvancedForm',
  mixins: [mixin, mixinDevice],
  components: {
    HeadInfo,
    MyPage
  },
  data () {
    return {
      description: this.$t('dataList.bhlsjl'),
      loading: false,
      dataLoading: false,
      curr_img_url: '',
      curr_img_show: false,
      stat: [
        0, 0, 0, 0, 0, 0
      ],
      // table
      columns: [
        {
          title: this.$t('dataList.zh'),
          dataIndex: 'username',
          key: 'username',
          scopedSlots: { customRender: 'username' }
        },
        {
          title: this.$t('dataList.je'),
          dataIndex: 'money',
          key: 'money'
        },
        {
          title: this.$t('dataList.czqye'),
          dataIndex: 'before',
          key: 'before',
          scopedSlots: { customRender: 'before' }
        },
        {
          title: this.$t('dataList.czhye'),
          dataIndex: 'after',
          key: 'after'
        },
        {
          title: this.$t('dataList.sj'),
          dataIndex: 'created_at',
          key: 'created_at'
        }
      ],
      data: [],
      errors: [],
      readyCnt: 0,
      runningCnt: 0,
      stopCnt: 0,
      curr_type: -1,
      curr_status: -1,
      curr_token: '',
      map_str: '',
      selectedId: [],
      loadingConfirm: false,
      page: {
        curr: 1,
        size: 20,
        total: 0
      },
      searchUsername: '',
      date_str: ''
    }
  },
  created () {
    this.getList()
    this.curr_token = this.$ls.get(ACCESS_TOKEN)
  },
  methods: {
    onSelectChange (selectedRowKeys) {
      this.selectedId = []
      for (let i = 0; i < selectedRowKeys.length; i++) {
        this.selectedId.push(this.data[selectedRowKeys[i]].id)
      }
      console.log(this.selectedId)
    },
    getList () {
      let url = '/user/recharge_list?page=' + this.page.curr + '&size=' + this.page.size
      if (this.searchUsername) {
        url += '&username=' + this.searchUsername
      }
      if (this.date_str) {
        url += '&date_str=' + this.date_str
      }
      this.dataLoading = true
      this.$http.get(url).then(res => {
        this.data = res
      }).catch(err => {
        this.$notification.error({
          message: this.$t('dataList.sb'),
          description: err.message
        })
      }).then(() => {
        this.dataLoading = false
      })
    },
    onSearch (val) {
      if (this.dataLoading) {
        return
      }
      this.searchUsername = val
      this.getList()
    },
    handleSubmit (e) {
      e.preventDefault()
    },
    handleChangeType (e) {
      this.curr_type = e.target.value
      this.getList()
      this.map_str = '&type=' + this.curr_type
      if (this.curr_status != -2) {
        this.map_str += '&status=' + this.curr_status
      }
    },
    handleChangeStatus (e) {
      this.curr_status = e.target.value
      this.getList()
      this.map_str = '&status=' + this.curr_status
      if (this.curr_type != -1) {
        this.map_str += '&type=' + this.curr_type
      }
    },
    handleClickChangeDate (e, str) {
      this.date_str = str
      this.getList()
    },
    handleChangePageSize (size) {
      this.page.size = size
      this.getList()
    },
    handleChangePage (page) {
      this.page.curr = page
      this.getList()
    }
  }
}
</script>

<style lang="less" scoped>
  .card{
    margin-bottom: 24px;
  }
</style>
